package com.bgs.service.impl;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bgs.common.R;
import com.bgs.config.DirectExchangeTest;
import com.bgs.fegin.OneFeign;

import com.bgs.pojo.Order;
import com.bgs.mapper.OrderMapper;
import com.bgs.pojo.vo.OrderTow;
import com.bgs.pojo.vo.OneOrderAndDetail;
import com.bgs.pojo.vo.FourOrderMidd;
import com.bgs.pojo.vo.chatData;
import com.bgs.pojo.vo.series;
import com.bgs.pojo.vo.FourOrderMidd;
import com.bgs.pojo.vo.chatData;
import com.bgs.pojo.vo.series;
import com.bgs.pojo.vo.*;
import com.bgs.pojo.vo.FourOrderMidd;
import com.bgs.pojo.vo.chatData;
import com.bgs.pojo.vo.series;
import com.bgs.service.OrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageBuilder;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * <p>
 * 订单 服务实现类
 * </p>
 *
 * @author Asia
 * @since 2021-09-14
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {
    @Resource
    private OrderMapper orderMapper;
    @Resource
    private OneFeign oneFeign;
    @Resource
    private RabbitTemplate rabbitTemplate;


    /**
     * 查看用户购买商品订单
     * @param beforeUserId
     * @return
     */
    @Override
    public List<OrderTow> TowGetOrder(Integer beforeUserId) {
        return orderMapper.TowGetOrder(beforeUserId);
    }

    /**
     * 用户进行退货
     * @param beforeUserId
     * @return
     */
    @Override
    public int TowOrderUp(Integer beforeUserId) {
        return orderMapper.TowOrderUp(beforeUserId);
    }
    @Override
    public R OneOrderrUserId(Integer beforeUserId) {
//        /*根据用户id查询订单*/
        List<OneOrderAndDetail> list = orderMapper.OneOrderrUserId(beforeUserId);
        for (OneOrderAndDetail oneOrderAndDetail : list) {
            List<DetailOrPicture> dp = oneFeign.OneGetDetailId(oneOrderAndDetail.getOrderDetailId());
            oneOrderAndDetail.setDp(dp);
        }
        return R.ok().data("list", list);
    }

    @Override
    public R OneOrderrUserIdByStatus0(Integer beforeUserId) {
        /*根据用户id查询订单状态为0 待付款*/
        List<OneOrderAndDetail> list = orderMapper.OneOrderrUserIdByStatus0(beforeUserId);
        for (OneOrderAndDetail oneOrderAndDetail : list) {
            List<DetailOrPicture> dp = oneFeign.OneGetDetailId(oneOrderAndDetail.getOrderDetailId());
            oneOrderAndDetail.setDp(dp);
        }
        return R.ok().data("list",list);
    }

    @Override
    public R OneOrderrUserIdByStatus1(Integer beforeUserId) {
        /*根据用户id查询订单状态为1 待付款*/
        List<OneOrderAndDetail> list = orderMapper.OneOrderrUserIdByStatus1(beforeUserId);
        for (OneOrderAndDetail oneOrderAndDetail : list) {
            List<DetailOrPicture> dp = oneFeign.OneGetDetailId(oneOrderAndDetail.getOrderDetailId());
            oneOrderAndDetail.setDp(dp);
        }
        return R.ok().data("list",list);
    }

    @Override
    public R OneOrderrUserIdByStatus2(Integer beforeUserId) {
        /*根据用户id查询订单状态为2 待付款*/
        List<OneOrderAndDetail> list = orderMapper.OneOrderrUserIdByStatus2(beforeUserId);
        for (OneOrderAndDetail oneOrderAndDetail : list) {
            List<DetailOrPicture> dp = oneFeign.OneGetDetailId(oneOrderAndDetail.getOrderDetailId());
            oneOrderAndDetail.setDp(dp);
        }
        return R.ok().data("list",list);
    }

    @Override
    public R OneOrderrUserIdByStatus3(Integer beforeUserId) {
        /*根据用户id查询订单状态为3 待付款*/
        List<OneOrderAndDetail> list = orderMapper.OneOrderrUserIdByStatus3(beforeUserId);
        for (OneOrderAndDetail oneOrderAndDetail : list) {
            List<DetailOrPicture> dp = oneFeign.OneGetDetailId(oneOrderAndDetail.getOrderDetailId());
            oneOrderAndDetail.setDp(dp);
        }
        return R.ok().data("list",list);
    }

    @Override
    public R OneOrderrUserIdByStatus4(Integer beforeUserId) {
        /*根据用户名的id查询用户的订单状态4退款/售后*/
        List<OneOrderAndDetail> list = orderMapper.OneOrderrUserIdByStatus4(beforeUserId);
        for (OneOrderAndDetail oneOrderAndDetail : list) {
            List<DetailOrPicture> dp = oneFeign.OneGetDetailId(oneOrderAndDetail.getOrderDetailId());
            oneOrderAndDetail.setDp(dp);
        }
        return R.ok().data("list",list);

    }

    @Override
    public R OneOrderrUserIdByStatus5(Integer beforeUserId) {
        /*根据用户名的id查询用户的订单状态5交易关闭*/
        List<OneOrderAndDetail> list = orderMapper.OneOrderrUserIdByStatus5(beforeUserId);
        for (OneOrderAndDetail oneOrderAndDetail : list) {
            List<DetailOrPicture> dp = oneFeign.OneGetDetailId(oneOrderAndDetail.getOrderDetailId());
            oneOrderAndDetail.setDp(dp);
        }
        return R.ok().data("list",list);
    }

    @Override
    public boolean updateByOrderId(Integer orderId) {
       return orderMapper.updateByOrderId(orderId);
    }



    @Override
    public R fourQueryOrder(Integer id) {
        List<FourOrderMidd> list = orderMapper.fourQueryOrder(id);

        String[] categories = new String[list.size()];
        Integer[] data = new Integer[list.size()];
        String name = "销量";
        series aa = new series();
        aa.setName(name);
        aa.setData(data);
        for (int i = 0; i < list.size(); i++) {
            categories[i]=list.get(i).getCategories();
            data[i]=list.get(i).getData();
        }
        chatData chatData = new chatData();
        chatData.setCategories(categories);
        chatData.setSeries(aa);
        return R.ok().data("data",chatData);
    }


    /**
     * 查询已退货的商品记录
     * @param beforeUserId
     * @return
     */
    @Override
    public List<OrderTow> TowTuiOrder(Integer beforeUserId) {
        return orderMapper.TowTuiOrder(beforeUserId);
    }

    @Override
    public R queenOrder(Map map) {

        String uuid = UUID.randomUUID().toString();
        String orderThree = JSONObject.toJSONString(map.get("orderThree"));
        ThreeJsonApp threeJsonApp = JSONObject.parseObject(orderThree, ThreeJsonApp.class);
        threeJsonApp.setUuid(uuid);
        String s = JSON.toJSONString(threeJsonApp);
        Message message = MessageBuilder.withBody(s.getBytes()).build();
        rabbitTemplate.convertAndSend(
                DirectExchangeTest.MONEY_DIRECT_EXCHANGE_NAME,
                DirectExchangeTest.MONEY_DIRECT_EXCHANGE_ROUTING_KEY_NAME,
                message
        );
        rabbitTemplate.convertAndSend(
                DirectExchangeTest.REPERTORY_DIRECT_EXCHANGE_NAME,
                DirectExchangeTest.REPERTORY_DIRECT_EXCHANGE_ROUTING_KEY_NAME,
                message
        );
        orderMapper.queenOrder(threeJsonApp);
        Integer orderId = threeJsonApp.getOrderId();
        threeJsonApp.setOrderId(orderId);
        orderMapper.ThreeAddZ(threeJsonApp);
        ThreeSelectIOIO list = orderMapper.selectThreeAdd(uuid);
        return R.ok().data("data", list);
    }
}
